#include "settingview.h"
#include "toolkit.h"
#include "resources.h"
#include "item/imageitem.h"
#include <QMouseEvent>
#include <QLocale>
#include "view/helpview.h"
#include "view/aboutview.h"
enum SetBtnTp
{
    HELP_SBTP,
    ABOUT_SBTP,
    BACK_SBTP,
    NULL_SBTP,
}setBtnTp(NULL_SBTP);

SettingView::SettingView(QWidget *parent) :
    QGraphicsView(parent),
    m_helpView(NULL),
    m_aboutView(NULL)
{
    m_Scene = new QGraphicsScene(this);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setScene(m_Scene);
    m_Scene->setSceneRect(0.0, 0.0, 640.0, 360.0);
    setGeometry(0.0,0.0,640.0,360.0);

    m_hintItem = new ImageItem(ToolKit::m_rc->m_hintImage,
                           QRect(QPoint(0,0),ToolKit::m_rc->m_hintImage.size()));
    m_hintItem->setZValue(1.0);
    m_hintItem->setVisible(false);
    m_Scene->addItem(m_hintItem);

    switch(ToolKit::m_language)
    {
    case QLocale::Chinese:
    {
        m_bgItem = new ImageItem(QImage(":/image/setting/setbg3.png"),
                                 QRect(QPoint(0,0),QImage(":/image/setting/setbg3.png").size()));
    }
        break;
    case QLocale::HongKong:
    case QLocale::Taiwan:
    {
        m_bgItem = new ImageItem(QImage(":/image/setting/setbg2.png"),
                                 QRect(QPoint(0,0),QImage(":/image/setting/setbg2.png").size()));
    }
        break;
    default:
    {
        m_bgItem = new ImageItem(QImage(":/image/setting/setbg1.png"),
                                 QRect(QPoint(0,0),QImage(":/image/setting/setbg1.png").size()));
    }
        break;
    }
    m_bgItem->setPos(0.0,0.0);
    m_bgItem->setZValue(0.0);
    m_Scene->addItem(m_bgItem);
}

SettingView::~SettingView()
{
    delete m_Scene;
    m_Scene = NULL;
}

void SettingView::helpback()
{
    if(m_helpView != NULL)
    {
        m_helpView->setVisible(false);
        delete m_helpView;
        m_helpView = NULL;
    }
}

void SettingView::mousePressEvent(QMouseEvent *event)
{
    if(QRectF(290,208,75,30).contains(event->pos()))
    {
        if(ToolKit::m_bVoice)
            ToolKit::playSound(4);

        setBtnTp = HELP_SBTP;
        m_hintItem->setPos(271.0,211.0);
        m_hintItem->setVisible(true);
    }
    else if(QRectF(290,250,90,30).contains(event->pos()))
    {
        if(ToolKit::m_bVoice)
            ToolKit::playSound(4);

        setBtnTp = ABOUT_SBTP;
        m_hintItem->setPos(271.0,253.0);
        m_hintItem->setVisible(true);
    }
    else if(QRectF(290,290,70,30).contains(event->pos()))
    {
        if(ToolKit::m_bVoice)
            ToolKit::playSound(4);

        setBtnTp = BACK_SBTP;
        m_hintItem->setPos(271.0,292.0);
        m_hintItem->setVisible(true);
    }
}

void SettingView::mouseReleaseEvent(QMouseEvent *event)
{
    m_hintItem->setVisible(false);
    switch(setBtnTp)
    {
    case HELP_SBTP:
    {
        if(QRectF(290,208,75,30).contains(event->pos()))
        {
            if(m_helpView == NULL)
            {
                m_helpView = new HelpView(this);
                connect(m_helpView,SIGNAL(back()),this,SLOT(helpback()));
                m_helpView->showFullScreen();
            }
        }
    }
        break;
    case ABOUT_SBTP:
    {
        if(QRectF(290,250,90,30).contains(event->pos()))
        {
            if(m_aboutView == NULL)
            {
                m_aboutView = new aboutView(this);
                m_aboutView->showFullScreen();
                connect(m_aboutView,SIGNAL(back()),this,SLOT(aboutback()));
            }
        }
    }
        break;
    case BACK_SBTP:
    {
        if(QRectF(290,290,70,30).contains(event->pos()))
        {
            emit back();
        }
    }
        break;
    default:
        break;
    }

    setBtnTp = NULL_SBTP;
}

void SettingView::aboutback()
{
    if(m_aboutView != NULL)
    {
        m_aboutView->setVisible(false);
        delete m_aboutView;
        m_aboutView = NULL;
    }
}


